# Copyright owners: Gentoo Foundation
#                   Arfrever Frehtes Taifersar Arahesis
# Distributed under the terms of the GNU General Public License v2

EAPI="5-progress"
PYTHON_ABI_TYPE="multiple"
PYTHON_RESTRICTED_ABIS="*-jython *-pypy"

inherit distutils eutils flag-o-matic fortran-2 multilib toolchain-funcs

MY_P="${PN}-${PV/_/}"
DOC_PV="0.15.1"
DOC_P="${PN}-${DOC_PV}"

DESCRIPTION="Scientific algorithms library for Python"
HOMEPAGE="http://www.scipy.org/ https://github.com/scipy/scipy https://pypi.python.org/pypi/scipy"
SRC_URI="mirror://sourceforge/${PN}/${MY_P}.tar.gz
	doc? (
		http://docs.scipy.org/doc/${DOC_P}/${PN}-html-${DOC_PV}.zip
		http://docs.scipy.org/doc/${DOC_P}/${PN}-ref-${DOC_PV}.pdf
	)"

LICENSE="BSD LGPL-2"
SLOT="0"
IUSE="doc sparse test"
KEYWORDS="*"

CDEPEND="$(python_abi_depend "dev-python/numpy[lapack]")
	sci-libs/arpack:0=
	virtual/cblas
	virtual/lapack
	sparse? ( sci-libs/umfpack:0= )"
DEPEND="${CDEPEND}
	dev-lang/swig
	$(python_abi_depend ">=dev-python/cython-0.19")
	virtual/pkgconfig
	test? ( $(python_abi_depend dev-python/nose) )"
RDEPEND="${CDEPEND}
	$(python_abi_depend dev-python/imaging)"

S="${WORKDIR}/${MY_P}"

DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES="1"
DOCS="THANKS.txt"

pkg_setup() {
	fortran-2_pkg_setup
	python_pkg_setup
}

src_unpack() {
	unpack ${MY_P}.tar.gz
	if use doc; then
		unzip -qo "${DISTDIR}/${PN}-html-${DOC_PV}.zip" -d html || die
	fi
}

pc_incdir() {
	$(tc-getPKG_CONFIG) --cflags-only-I $@ | \
		sed -e 's/^-I//' -e 's/[ ]*-I/:/g' -e 's/[ ]*$//' -e 's|^:||'
}

pc_libdir() {
	$(tc-getPKG_CONFIG) --libs-only-L $@ | \
		sed -e 's/^-L//' -e 's/[ ]*-L/:/g' -e 's/[ ]*$//' -e 's|^:||'
}

pc_libs() {
	$(tc-getPKG_CONFIG) --libs-only-l $@ | \
		sed -e 's/[ ]-l*\(pthread\|m\)\([ ]\|$\)//g' \
		-e 's/^-l//' -e 's/[ ]*-l/,/g' -e 's/[ ]*$//' \
		| tr ',' '\n' | sort -u | tr '\n' ',' | sed -e 's|,$||'
}

src_prepare() {
	# Support Python 3.1.
	sed -e "/sys.version_info/s/(3, 2)/(3, 1)/" -i setup.py

	# scipy automatically detects libraries by default
	export {FFTW,FFTW3,UMFPACK}=None
	use sparse && unset UMFPACK
	# the missing symbols are in -lpythonX.Y, but since the version can
	# differ, we just introduce the same scaryness as on Linux/ELF
	[[ ${CHOST} == *-darwin* ]] \
		&& append-ldflags -bundle "-undefined dynamic_lookup" \
		|| append-ldflags -shared
	[[ -z ${FC}  ]] && export FC="$(tc-getFC)"
	# hack to force F77 to be FC until bug #278772 is fixed
	[[ -z ${F77} ]] && export F77="$(tc-getFC)"
	export F90="${FC}"
	SCIPY_FCONFIG="config_fc --noopt --noarch"
	append-fflags -fPIC

	local libdir="${EPREFIX}"/usr/$(get_libdir)
	cat >> site.cfg <<-EOF
		[blas]
		include_dirs = $(pc_incdir cblas)
		library_dirs = $(pc_libdir cblas blas):${libdir}
		blas_libs = $(pc_libs cblas blas)
		[lapack]
		library_dirs = $(pc_libdir lapack):${libdir}
		lapack_libs = $(pc_libs lapack)
	EOF

	distutils_src_prepare

	preparation() {
		# Regenerate Cython-generated files.
		eshopts_push -s extglob
		local file
		for file in $(grep -El "^/\* Generated by Cython .* \*/$" **/*.@(c|cxx)); do
			python_execute cython $([[ ${file} == *.cxx ]] && echo --cplus) ${file%.*}.pyx -o ${file} || die "Cythonization of ${file%.*}.pyx failed"
		done
		eshopts_pop
	}
	python_execute_function -s preparation
}

distutils_src_compile_post_hook() {
	if [[ "$(python_get_version -l)" == "3.1" ]]; then
		2to3-${PYTHON_ABI} -f callable -nw --no-diffs build/lib*
	fi
}

src_compile() {
	distutils_src_compile ${SCIPY_FCONFIG}
}

src_test() {
	testing() {
		python_execute "$(PYTHON)" setup.py ${SCIPY_FCONFIG} install --root="${T}/tests-${PYTHON_ABI}" || die "Installation for tests failed with $(python_get_implementation_and_version)"
		pushd "${T}/tests-${PYTHON_ABI}" > /dev/null || die
		python_execute PYTHONPATH="${T}/tests-${PYTHON_ABI}${EPREFIX}$(python_get_sitedir)" "$(PYTHON)" -c "import scipy, sys; sys.exit(not scipy.test(label='full', verbose=3).wasSuccessful())" || return
		popd > /dev/null || die
	}
	python_execute_function -s testing
}

src_install() {
	distutils_src_install ${SCIPY_FCONFIG}

	if use doc; then
		dohtml -r "${WORKDIR}/html/"
		dodoc "${DISTDIR}/${PN}-ref-${DOC_PV}.pdf"
	fi
}

pkg_postinst() {
	distutils_pkg_postinst
	elog "You might want to set the variable SCIPY_PIL_IMAGE_VIEWER"
	elog "to your prefered image viewer. Example:"
	elog "\t echo \"export SCIPY_PIL_IMAGE_VIEWER=display\" >> ~/.bashrc"
}
